#leetcode题目98：验证二叉搜索树
#难度：中等
#时间复杂度：O(n)
#空间复杂度：O(n)
#方法：递归


class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def isValidBST(self, root: TreeNode) -> bool:
        def valid(root,left,right):
            if not root:
                return True
            if root.val<=left or root.val>=right:
                return False
            return valid(root.left,left,root.val) and valid(root.right,root.val,right)
        return valid(root,float('-inf'),float('inf'))

#测试数据
root=[2,1,3]
root=TreeNode(2,TreeNode(1,None,None),TreeNode(3,None,None))
#预期输出：True
solution=Solution()
print(solution.isValidBST(root))

root=[5,1,4,None,None,3,6]
root=TreeNode(5,TreeNode(1,None,None),TreeNode(4,TreeNode(3,None,None),TreeNode(6,None,None)))
#预期输出：False
solution=Solution()
print(solution.isValidBST(root))